////////////////////////////////////////////////
// DESCRIPTION:
//     Class implementationn for device driver
//     access to registry values
//
// Legal Notices:
//     Copyright (c) 2008, Telliam Consulting, LLC.
//     All rights reserved.
//
//     Redistribution and use in source and binary forms, with or without modification,
//     are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//
//     * Redistributions in binary form must reproduce the above copyright notice,
//       this list of conditions and the following disclaimer in the documentation
//       and/or other materials provided with the distribution.
//
//     * Neither the name of Telliam Consulting nor the names of its contributors
//       may be used to endorse or promote products derived from this software
//       without specific prior written permission.
//
// Disclaimer:
//     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
//     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//     IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
//     INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
//     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//     LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
//     OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
//     OF THE POSSIBILITY OF SUCH DAMAGE.  
//

#include <wtypes.h>
#include <FusionWare\DFX\StreamAPI.h>

using namespace FusionWare::DFX;

DeviceRegValues::DeviceRegValues()
{
}

DeviceRegValues::~DeviceRegValues()
{
}

// REVIEW: Look into reading all the IRQ info values 
//         in the same way as the Memory and I/O windows
bool DeviceRegValues::ReadIrqValues( FusionWare::Win32::RegKey& DevKey
                                   , int NumIrqs 
                                   , const TCHAR* pIrqSubKeyNames[]
                                   )
{
    // if no IRQs requested/expected then do nothing
    if(0 == NumIrqs)
        return true;

    this->Irqs = new IrqInfo[NumIrqs];
    if(this->Irqs == NULL)
    {
        DEBUGMSG(1, (_T("DeviceRegValues::ReadRegValues - FAILED to allocate memory for IRQ info.\n")));
        SetLastError(ERROR_OUTOFMEMORY);
        return false;
    }

    // The first irq is always listed in the root
    if(!Irqs[0].ReadRegValues(DevKey))
    {
        DEBUGMSG(1, (_T("Could not load IRQ settings from driver root key\n")));
        return false;
    }

    // subsequent IRQs are loaded from subkeys
    for(int i = 1; i < NumIrqs; ++i)
    {
        FusionWare::Win32::RegKey subKey;
        if(!subKey.Open(DevKey, pIrqSubKeyNames[i-1]))
        {
            DEBUGMSG(1, (_T("Could not open IRQ settings subkey: \"%s\"\n"), pIrqSubKeyNames[i-1]));
            return false;
        }

        if(!Irqs[i].ReadRegValues(subKey))
        {
            DEBUGMSG(1, (_T("Could not load IRQ settings from subkey: \"%s\"\n"), pIrqSubKeyNames[i-1]));
            return false;
        }
    }

    return true;
}

bool DeviceRegValues::ReadRegValues( const TCHAR* pRegPath
                                   , int NumIrqs /*=0*/
                                   , const TCHAR* pIrqSubKeyNames[]/*=NULL*/
                                   )
{
    FusionWare::Win32::RegKey devKey;

    this->ParentBus.Open(pRegPath);
    
    if(!devKey.OpenDeviceKey(pRegPath))
    {
        // If the device key failed just try the reg path directly.
        // Native drivers and NDIS drivers don't use the Device key
        // for storing the driver config data. 
        if(!devKey.Open(HKEY_LOCAL_MACHINE, pRegPath))
            return false;
    }
    
    // read in the direct data members
    if(!RegValues::ReadRegValues(devKey))
        return false;

    // load in the memory and I/O window info
    if(!Windows.LoadFromRegistry(devKey, this->ParentBus))
        return false;

    // lastly get the IRQ info
    return ReadIrqValues(devKey, NumIrqs, pIrqSubKeyNames);
}

